package br.com.automed.validator;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value="com.automed.dataHistoricoValidator")
public class DataHistoricoValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		
		UIInput input = (UIInput) component.findComponent("dataFinal");	
		Date dtInicial = (Date)value;		
		Date dtFinal = (Date) input.getValue();

		if(dtInicial != null && dtFinal != null){
						
			if(dtInicial.after(dtFinal)){
				FacesMessage message = new FacesMessage();
				message.setSeverity(FacesMessage.SEVERITY_ERROR);
				message.setSummary("Data inicial maior que a final");
				
				throw new ValidatorException(message);
			}
		}
		
		
		
	}

}
